package com.teamclub.domain.erp.storage;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.teamclub.domain.erp.product.ProductSpecs;
import io.ebean.Ebean;
import io.ebean.Model;
import com.teamclub.util.libs.Libs;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * Created by ilkkzm on 17-10-9.
 */

@Entity
@Table(name="erp_storage_record_detail")
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
public class StorageRecordDetail extends Model{
    @Id
    private Integer id;
    private String producerOrganNo;
    private String dealerOrganNo;
    private String batchNo;
    private String erCode;
    private Integer operatorId;
    private Integer recordType; // '1 入库　２　出库　３退货'

    @Transient
    private String erBatchData;
    @Transient
    private String erMaterialBox;
    @Transient
    private String erCodeName;
    @Transient
    private String erCodeSpec;

    @Transient
    @JsonIgnore
    private StorageRecord storageRecord;

    @Transient
    private String formattedActionTime;

    @Transient
    private String storageName;

    @Transient
    private String linkCustomer;

    public String getErMaterialBox() {
        if(StringUtils.isBlank(this.erCode)) {
            return "";
        }
        if(this.erCode.length() == 20) {
            return this.erCode.substring(14, 20);
        } else if(this.erCode.length() == 22) {
            return this.erCode.substring(16, 22);
        } else if(this.erCode.length() == 23) {
            return this.erCode.substring(17, 23);
        } else {
            return "";
        }

    }

    public String getErCodeName() {
        if(StringUtils.isBlank(this.erCode)) {
            return "";
        }
        if(this.erCode.length() >= 5) {
            ProductSpecs entity = Ebean.find(ProductSpecs.class).where().eq("specsCode", this.erCode.substring(0, 5)).eq("organNo", this.getProducerOrganNo()).findOne();
            if(entity == null) {
                return null;
            }
            return entity.getSpecsName();
        } else {
            return "";
        }
    }

    public String getErBatchData() {
        if(StringUtils.isBlank(this.erCode)) {
            return "";
        }
        if(this.erCode.length() == 20) {
            return "20" + this.erCode.substring(5, 14);
        } else if(this.erCode.length() == 22) {
            return "20" + this.erCode.substring(5, 16);
        } else if(this.erCode.length() == 23) {
            return "20" + this.erCode.substring(5, 17);
        } else {
            return "";
        }
    }
    public StorageRecord getStorageRecord() {
        if(this.storageRecord == null) {
            StorageRecord instance = Ebean.find(StorageRecord.class).where().eq("producerOrganNo", this.producerOrganNo).eq("dealerOrganNo", dealerOrganNo).eq("batchNo", this.batchNo).eq("operatorId", this.operatorId).findOne();
            this.storageRecord = instance;
        }
        return this.storageRecord;
    }
}
